package com.htc.dlc;

import java.io.File;

import com.htc.constants.Constants;
import com.htc.menu.MenuController;
import com.htc.menu.MenuController.MenuSelectedListener;
import com.htc.utils.CommonActions;
import com.htc.webservice.PicassoUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DetailEventActivity extends Activity implements MenuSelectedListener{
	TextView tv_actionbar_title;
	MenuController menuController;
	View menuView;
	View menuViewSecondary;
	LinearLayout blurimageView;
	CommonActions ca;
	TextView txtTitleEvent;
	ImageView imgEvent;
	WebView webviewDescription;
	LinearLayout lnContent;
	Button back_btn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_event);
		this.ca = new CommonActions(this);
		initViews();
		this.menuController = new MenuController(this.menuView,
				this.menuViewSecondary, this.blurimageView, this,
				getBaseContext(), DetailEventActivity.this);

		Intent i = getIntent();
		Bundle data = i.getExtras();
		String title = data.getString("title");
		String description = data.getString("description").toString().replaceAll("\\\\n", "\n");
		txtTitleEvent.setText(title);
		String encoding = "utf-8";
		this.webviewDescription.getSettings().setJavaScriptEnabled(true);
		this.webviewDescription.loadDataWithBaseURL(null, description, null, encoding, null);
		
		String path = data.getString("image");
		String currentUrl = Constants.URL_IMAGE_PAHT + path;

		String[] strs = path.split("/");
		File f = new File(Environment.getExternalStorageDirectory() + "/"
				+ Constants.APP_NAME + "/" + strs[0] + "/" + strs[1]);
		if (f.exists()) {
			if(f.length() == 0){
				f.delete();
				PicassoUtils.getInstance().showImageFromUrl(DetailEventActivity.this, currentUrl,
						R.drawable.bgday, imgEvent);
				
			}else{
				Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
				imgEvent.setImageBitmap(bmp);
			}
			
		} else {
			PicassoUtils.getInstance().showImageFromUrl(DetailEventActivity.this, currentUrl,
					R.drawable.bgday, imgEvent);
		}
	}
	
	public void initViews(){
		this.back_btn = (Button)findViewById(R.id.back_btn);
		this.back_btn.setVisibility(1);
		this.back_btn.setOnClickListener(new OnClickListener() {
		
			@Override
			public void onClick(View v) {
				int id = v.getId();
				switch (id) {
				case R.id.back_btn:{
					 finish();
					 overridePendingTransition( R.anim.fade_in, R.anim.fade_out );
					// getActivity().setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
					
				}
					break;

				default:
					break;
				}
				// TODO Auto-generated method stub
				
			}
		});
		tv_actionbar_title = (TextView)findViewById(R.id.tv_actionbar_title);
		tv_actionbar_title.setText("Details of Event");
		this.lnContent = (LinearLayout)findViewById(R.id.lnContent);
		this.menuView = findViewById(R.id.menu_container);
		this.menuViewSecondary = findViewById(R.id.menu_secondary);
		this.blurimageView = (LinearLayout) findViewById(R.id.lay_blur_image);
		txtTitleEvent = (TextView)findViewById(R.id.txtTitleEvent);
		imgEvent = (ImageView)findViewById(R.id.imgEvent);
		webviewDescription = (WebView)findViewById(R.id.webviewDescription);
	}
	
	@Override
	public void onMenuSelected(int paramInt) {
		// TODO Auto-generated method stub

	}
}
